/**
 * 
 */
package com.flashdeck.dao.impl;

import java.io.File;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

import com.flashdeck.dao.SettingsDao;

/**
 * @author Vincentas
 *
 */
public class SettingsDaoImpl implements SettingsDao {

	private static final String DECK_FOLDER = "decksPath";
	
	private final Context context;
	
	public SettingsDaoImpl(Context context) {
		this.context = context;
	}
	
	@Override
	public File getDeckFolder() {
		File externalStorageDirectory = Environment.getExternalStorageDirectory();
		
		SharedPreferences preferences = getContext().getSharedPreferences("prefs", Context.MODE_PRIVATE);
		String folderPath = preferences.getString(DECK_FOLDER, "/decks");
		
		File result = new File(externalStorageDirectory.getAbsoluteFile() + folderPath);
		if (result.exists()) {
			if (result.isFile()) {
				throw new Error("Deck folder is invalid (file) " + result.getAbsolutePath());
			}
		} else {
			boolean created = result.mkdirs();
			if (!created) {
				throw new Error("Unable to create folder " + result.getAbsolutePath());
			}
		}
		
		return result;
	}

	/**
	 * @return the context
	 */
	public Context getContext() {
		return context;
	}
}
